package com.fishery.refrigeratory.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.refrigeratory.entity.Refrigeratory;
import com.fishery.refrigeratory.entity.vo.ProductVo;
import com.fishery.refrigeratory.mapper.RefrigeratoryMapper;
import com.fishery.refrigeratory.service.RefrigeratoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

/**
 * @author wgz
 * @since 2021/1/20
 */

@Service
public class RefrigeratoryServiceImpl extends ServiceImpl<RefrigeratoryMapper, Refrigeratory> implements RefrigeratoryService {

    @Autowired
    private RefrigeratoryMapper refrigeratoryMapper;

    /**
     * 创建冷库
     * @param refrigeratory
     * @return
     */
    @Override
    public boolean createRefrigeratory(Refrigeratory refrigeratory) {
        return refrigeratoryMapper.insert(refrigeratory) != 0? true: false;
    }

    /**
     * 修改冷库
     * @param refrigeratory
     * @return
     */
    @Override
    public boolean updateRefrigeratory(Refrigeratory refrigeratory) {
        return refrigeratoryMapper.updateById(refrigeratory) != 0? true: false;
    }

    /**
     * 删除冷库
     * @param id
     * @return
     */
    @Override
    public boolean deleteRefrigeratory(String id) {
        return refrigeratoryMapper.deleteById(id) != 0? true: false;
    }

    /**
     * 根据冷库id查询冷库
     * @param id
     * @return
     */
    @Override
    public Refrigeratory getRefrigeratory(String id) {
        QueryWrapper<Refrigeratory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return refrigeratoryMapper.selectOne(queryWrapper);
    }

    /**
     * 根据基地id获取冷库列表
     * @param baseId
     * @return
     */
    @Override
    public List<Refrigeratory> getRefrigeratoryByBaseId(String baseId) {
        QueryWrapper<Refrigeratory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("base_id",baseId);
        return refrigeratoryMapper.selectList(queryWrapper);
    }


    /**
     * 查询所有的冷库信息
     * @return
     */
    @Override
    public List<Refrigeratory> getAllRefrigeratory() {
        return refrigeratoryMapper.selectList(null);
    }

    /**
     * 根据基地id分页查询冷库
     * @param base_id
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Refrigeratory> PageRefrigeratoryByBaseId(String base_id, Integer page, Integer size) {
        Page<Refrigeratory> refrigeratoryPage = new Page<>(page, size);
        QueryWrapper<Refrigeratory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("base_id", base_id);
        refrigeratoryMapper.selectPage(refrigeratoryPage, queryWrapper);
        return refrigeratoryPage;
    }

    /**
     * 得到所有的分页冷库
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Refrigeratory> getAllPageRefrigeratory(Integer page, Integer size) {
        Page<Refrigeratory> refrigeratoryPage = new Page<>(page, size);
        refrigeratoryMapper.selectPage(refrigeratoryPage, null);
        return refrigeratoryPage;
    }

    /**
     * 通过基地id获取所有冷库的面积
     * @param baseId
     * @return
     */
    @Override
    public Integer selectAllAreaByBaseId(String baseId) {
        QueryWrapper<Refrigeratory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(refrigeratory_area) as totalarea")
                .eq("base_id", baseId);
        Map<String, Object> map = this.getMap(queryWrapper);
        Integer totalarea = (Integer) map.get("totalarea");
        return totalarea;
    }

    /**
     * 通过基地id获取所有冷库的数量
     * @param baseId
     * @return
     */
    @Override
    public long selectAllRefrigeratoryCount(String baseId) {
        QueryWrapper<Refrigeratory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(id) as totalcount")
                .eq("base_id", baseId);
        Map<String, Object> map = this.getMap(queryWrapper);
        long totalcount = (long) map.get("totalcount");
        return totalcount;
    }
}
